package com.ruoyi.project.orderSystem.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.orderSystem.domain.WorkOrder;
import com.ruoyi.project.orderSystem.mapper.WorkOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.orderSystem.mapper.WorkOrderAuditMapper;
import com.ruoyi.project.orderSystem.domain.WorkOrderAudit;
import com.ruoyi.project.orderSystem.service.IWorkOrderAuditService;

/**
 * 工单审核主Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-03-04
 */
@Service
public class WorkOrderAuditServiceImpl implements IWorkOrderAuditService 
{
    @Autowired
    private WorkOrderAuditMapper workOrderAuditMapper;
    @Autowired
    private WorkOrderMapper workOrderMapper;

    /**
     * 查询工单审核主
     * 
     * @param id 工单审核主ID
     * @return 工单审核主
     */
    @Override
    public WorkOrderAudit selectWorkOrderAuditById(Long id)
    {
        return workOrderAuditMapper.selectWorkOrderAuditById(id);
    }

    /**
     * 查询工单审核主列表
     * 
     * @param workOrderAudit 工单审核主
     * @return 工单审核主
     */
    @Override
    public List<WorkOrderAudit> selectWorkOrderAuditList(WorkOrderAudit workOrderAudit)
    {
        return workOrderAuditMapper.selectWorkOrderAuditList(workOrderAudit);
    }

    /**
     * 新增工单审核主
     * 
     * @param workOrderAudit 工单审核主
     * @return 结果
     */
    @Override
    public int insertWorkOrderAudit(WorkOrderAudit workOrderAudit)
    {
        return workOrderAuditMapper.insertWorkOrderAudit(workOrderAudit);
    }

    /**
     * 修改工单审核主
     * 
     * @param workOrderAudit 工单审核主
     * @return 结果
     */
    @Override
    public int updateWorkOrderAudit(WorkOrderAudit workOrderAudit)
    {
        return workOrderAuditMapper.updateWorkOrderAudit(workOrderAudit);
    }

    /**
     * 批量删除工单审核主
     * 
     * @param ids 需要删除的工单审核主ID
     * @return 结果
     */
    @Override
    public int deleteWorkOrderAuditByIds(Long[] ids)
    {
        return workOrderAuditMapper.deleteWorkOrderAuditByIds(ids);
    }

    /**
     * 删除工单审核主信息
     * 
     * @param id 工单审核主ID
     * @return 结果
     */
    @Override
    public int deleteWorkOrderAuditById(Long id)
    {
        return workOrderAuditMapper.deleteWorkOrderAuditById(id);
    }

    @Override
    public List<WorkOrder> workOrderUnAudit(WorkOrder workOrder) {
        workOrder.setOrderStatus(OrderStatus.ORDER_COMPLETED.getKey().intValue());
        return workOrderMapper.selectWorkOrderList(workOrder);
    }

    @Override
    public int auditWorkOrder(WorkOrderAudit workOrderAudit) {
        Optional.ofNullable(workOrderAudit.getOrderId()).orElseThrow(()-> new BaseException("参数异常"));
        WorkOrderAudit workOrderAudit1 = new WorkOrderAudit();
        workOrderAudit1.setOrderId(workOrderAudit.getOrderId());
        List<WorkOrderAudit> workOrderAudits = workOrderAuditMapper.selectWorkOrderAuditList(workOrderAudit1);
        if(workOrderAudits.size()>0){
            workOrderAudit.setGmtModify(new Date());
            workOrderAudit.setModifier(SecurityUtils.getLoginUser().getUser().getUserId());
            workOrderAuditMapper.updateWorkOrderAudit(workOrderAudit);
        }else {
            workOrderAudit.setGmtCreate(new Date());
            workOrderAudit.setAuditor(SecurityUtils.getLoginUser().getUser().getUserId());
            workOrderAuditMapper.insertWorkOrderAudit(workOrderAudit);
        }
        WorkOrder workOrder = new WorkOrder();
        workOrder.setId(workOrderAudit.getOrderId());
        if(workOrderAudit.getAuditStatus()==1){
            workOrder.setOrderStatus(OrderStatus.ORDER_IN_FILE.getKey().intValue());
        }else {
            workOrder.setOrderStatus(OrderStatus.ORDER_REJECT.getKey().intValue());
        }
        //更改工单状态
        return workOrderMapper.updateWorkOrder(workOrder);
    }
}
